
#include "Retractor.h"

int inin_Retractor(void) //初始化控制收放机的引脚
{
    wiringPiSetup();                // wiringPi库初始化
    softPwmCreate(pwm_pin, 0, 100); // 设置GPIO.1为PWM输出
    pinMode(just_R_pin, OUTPUT);    //设置引脚为输出模式
    pinMode(back_R_pin, OUTPUT);    //设置引脚为输出模式
    digitalWrite(just_R_pin, HIGH);
    digitalWrite(back_R_pin, HIGH);//初始化

    pthread_mutex_init(&mutex_retractor, NULL); //NULL为默认的互斥锁
    int val = 0;
    if ((pthread_create(&Retractor_id, NULL, Retractor, NULL) == -1))
    {
        printf("创建线程错误!\n");
        return -2;
    }
    return 0;
}

int isRetractor_cmd()
{
    pthread_mutex_lock(&mutex_retractor); //互斥锁加锁
    int Retractor_cmd_ = Retractor_cmd;
    pthread_mutex_unlock(&mutex_retractor); ////互斥锁解锁
    return Retractor_cmd_;
}

    void *
    Retractor(void *arg)
{
    pthread_detach(pthread_self()); //线程分离
     while (1)
    {
        printf("aaa=%d\n", isRetractor_cmd());
        if (isRetractor_cmd() == 0)
        {

            int var = 100;
            while (var > 0)
            {
                var--;
                printf("555===%d\n", var);
                softPwmWrite(pwm_pin, var);
                usleep(30000); //3秒软关闭，消除电机瞬间电流，保护继电器
            }
            digitalWrite(just_R_pin, HIGH);
            digitalWrite(back_R_pin, HIGH); //继电器复位
            while (isRetractor_cmd() == 0)
            {
               // printf("aaa=%d\n", Retractor_cmd);
                usleep(50000);
            }
        }
        else if (isRetractor_cmd() == 1)
        {
            int var = 100;
            while (var > 0)
            {
                var--;
                printf("555===%d\n", var);
                softPwmWrite(pwm_pin, var);
                usleep(30000); //3秒软关闭，消除电机瞬间电流，保护继电器
            }
            digitalWrite(just_R_pin, HIGH);
            digitalWrite(back_R_pin, LOW);//正转
            var=0;
            while (var<100)
            {
                var++;
                printf("111===%d\n",var);
                softPwmWrite(pwm_pin, var);
                usleep(30000); //3秒软启动启动，消除电机瞬间电流，保护继电器
            }
            while (isRetractor_cmd() == 1)
            {
                usleep(50000);
            }
        }
        else if (isRetractor_cmd() == 2)
        {
            int var = 100;
            while (var > 0)
            {
                var--;
                printf("555===%d\n", var);
                softPwmWrite(pwm_pin, var);
                usleep(30000); //3秒软关闭，消除电机瞬间电流，保护继电器
            }
            digitalWrite(just_R_pin, LOW);
            digitalWrite(back_R_pin, HIGH); //反转
             var = 0;
            while (var < 100)
            {
                var++;
                printf("222===%d\n", var);
                softPwmWrite(pwm_pin, var);
                usleep(30000); //3秒软启动启动，消除电机瞬间电流，保护继电器
            }
            while (isRetractor_cmd() == 2)
            {
                usleep(50000);
            }
        }
       
        usleep(50000);
    }
}

void set_Retractor_cmd(int cmd)
{
    pthread_mutex_lock(&mutex_retractor); //互斥锁加锁
    Retractor_cmd = cmd;
    printf("bbb=====%d\n", Retractor_cmd);
    pthread_mutex_unlock(&mutex_retractor); ////互斥锁解锁
}
